package se.rende.myrss;

import se.rende.myrss.data.ChannelParser;
import se.rende.myrss.data.Constants;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

public class ItemDetailFragment extends Fragment implements
		LoaderManager.LoaderCallbacks<Cursor>, OnClickListener {

	private static final int ITEM_LOADER = 0;
	public static final String ARG_ITEM_ID = "item_id";
	public static final String ARG_CHANNEL_ID = "channel_id";
	public static final String ARG_SHOW_NEW_ID = "show_new_id";

	private ChannelParser.ChannelItem item;
	private View rootView;

	public ItemDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments().containsKey(ARG_ITEM_ID)) {
			getLoaderManager().initLoader(ITEM_LOADER, getArguments(), this);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_item_detail, container,
				false);
		TextView titleView = (TextView) rootView.findViewById(R.id.item_title);
		titleView.setOnClickListener(this);
		return rootView;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
		boolean showNew = args.getBoolean(ARG_SHOW_NEW_ID);
		if (loaderId == ITEM_LOADER) {
			String itemId = args.getString(ARG_ITEM_ID);

			// read the current item
			String[] projection = { "quid", "channel", "title", "description",
					"link", "pub_date", "creator" };
			return new CursorLoader(getActivity(), Constants.CONTENT_URI_ITEM,
					projection, "_id=?" + (showNew ? " and seen=0" : ""),
					new String[] { itemId }, null);
		} else {
			throw new IllegalArgumentException("unknown loader id " + loaderId);
		}
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		if (loader.getId() == ITEM_LOADER) {
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					item = new ChannelParser.ChannelItem(cursor.getString(0),
							null, cursor.getString(1),
							cursor.getString(2), cursor.getString(3),
							cursor.getString(4), cursor.getLong(5), cursor.getString(6));
					((TextView) rootView.findViewById(R.id.item_title))
							.setText(item.title);
					((TextView) rootView.findViewById(R.id.item_date))
							.setText(DateFormat.getMediumDateFormat(
									getActivity()).format(item.pubDate)
									+ " "
									+ DateFormat.getTimeFormat(getActivity())
											.format(item.pubDate)
									+ " by "
									+ item.creator);
					((WebView) rootView.findViewById(R.id.item_description))
							.loadData(item.description,
									"text/html; charset=utf-8", "utf-8");
				}
				cursor.close();
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		((TextView) rootView.findViewById(R.id.item_title)).setText("");
		((TextView) rootView.findViewById(R.id.item_date)).setText("");
		((WebView) rootView.findViewById(R.id.item_description)).clearView();
	}

	public void openLink() {
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.link));
		startActivity(browserIntent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
        case R.id.item_title:
        	openLink();
        	break;
		}
	}
}
